package com.zlt.service.impl;

import com.alipay.api.domain.CompanyInfo;
import com.zlt.dao.CompanyDao;
import com.zlt.dao.Impl.CompanyDaoImpl;
import com.zlt.dao.Impl.RoleDaoImpl;
import com.zlt.dao.RoleDao;
import com.zlt.dto.CompanyDTO;
import com.zlt.entity.Company;
import com.zlt.entity.Role;
import com.zlt.entity.User;
import com.zlt.service.CompanyService;
import com.zlt.util.ActiveUserUtil;
import com.zlt.vo.ActiveUser;
import com.zlt.vo.CompanyVO;
import com.zlt.vo.Pager;
import com.zlt.vo.UserVo;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CompanyServiceImpl implements CompanyService {

    private CompanyDao companyDao = new CompanyDaoImpl();
    private RoleDao roleDao = new RoleDaoImpl();


    @Override
    public boolean createCompany(CompanyDTO companyDTO) {

        ActiveUser activeUser = ActiveUserUtil.get();
        System.out.println("createCompany:" + activeUser);

        if (activeUser == null) {
            throw new RuntimeException("当前未登录，无法创建");
        }
        Company company = new Company();

        try {
            BeanUtils.copyProperties(company, companyDTO);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        Company selectCompany = companyDao.selectCompanyByCname(company.getCname(), 0);

        if (selectCompany != null) {
            throw new RuntimeException("已注册企业");
        }

        Company com = companyDao.selectCompanyInfoByUid(activeUser.getUid(), 3);

        if (com != null) {
            throw new RuntimeException("已是企业管理员");
        }

        System.out.println("regCompany:" + company);


        //将信息放入数据库
        int cid = companyDao.insertCompany(company);

        //把注册好的 企业和管理员 放如user_company表中
        int success = companyDao.insertUserCompany(activeUser.getUid(), cid, 3);


        return success > 0;
    }

    @Override
    public Pager<UserVo> selectUserHr(Pager<UserVo> pager) {
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null) {
            throw new RuntimeException("当前未登录");
        }

        //查询总条数
        Long totalCount = companyDao.selectCountUserHr(activeUser.getUid());
        System.out.println("HrtotalCount:" + totalCount);
        //总条数
        pager.setTotalCount(totalCount);
        //总页数
        pager.setPageCount((pager.getTotalCount() - 1) / pager.getLimit() + 1);
        //查询当前页的数据

        //先去查当前活动人的公司是哪个
        Company company = companyDao.selectCompanyInfoByUid(activeUser.getUid());

        System.out.println("usercompany:" + company);

        List<User> users = companyDao.selectUserHrByCid(pager, company.getCid(), 0);
        System.out.println("allHr:" + users);
        List<UserVo> userVos = new ArrayList<>();

        users.forEach(e -> {
            UserVo userVo = new UserVo();
            userVos.add(userVo);
            try {
                BeanUtils.copyProperties(userVo, e);
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
            }

        });
        pager.setData(userVos);
        return pager;
    }

    @Override
    public Pager<UserVo> selectReviewedHr(Pager<UserVo> pager) {
        ActiveUser activeUser = ActiveUserUtil.get();
        System.out.println("companyUser：" + activeUser);

        if (activeUser == null) {
            throw new RuntimeException("当前未登录");
        }

        //分页查询一个总条数
        Long toalCount = companyDao.selectReviewedHrCount(activeUser.getUid());
        pager.setTotalCount(toalCount);

        //总页数
        pager.setPageCount((pager.getTotalCount() - 1) / pager.getLimit() + 1);

        Company company = companyDao.selectCompanyInfoByUid(activeUser.getUid());

        //查询这一页的数据
        List<User> users = companyDao.selectUserHr(pager, company.getCid(), 1);
        System.out.println("userHr:" + users);

        List<UserVo> userVos = new ArrayList<>();
        users.forEach(e -> {
            UserVo userVo = new UserVo();
            userVos.add(userVo);

            try {
                BeanUtils.copyProperties(userVo, e);
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
            }
        });
        pager.setData(userVos);
        return pager;
    }

    @Override
    public boolean updateUserCompanyState(String uid, Integer state) {
        return companyDao.updateUserCompanyState(uid, state) > 0;
    }

    @Override
    public Pager<CompanyVO> selectCompany(Pager<CompanyVO> pager) {
        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null) {
            throw new RuntimeException("当前未登录");
        }

        //查询总条数
        Long totalCount = companyDao.selectCount(activeUser.getUid());
        //总条数
        pager.setTotalCount(totalCount);
        //总页数
        pager.setPageCount((pager.getTotalCount() - 1) / pager.getLimit() + 1);
        //查询当前页的数据

        List<Company> companys = companyDao.selectCompanyInfoByUid(pager, activeUser.getUid());
        List<CompanyVO> companyVos = new ArrayList<>();

        companys.forEach(e -> {
            CompanyVO companyVo = new CompanyVO();
            companyVos.add(companyVo);
            try {
                BeanUtils.copyProperties(companyVo, e);
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
            }

        });
        pager.setData(companyVos);
        return pager;
    }
}
